// 实现日志宏模块

#ifndef __LOG__HPP
#define __LOG__HPP

/*[2025-08-06 22:00:54] [INFO] [main.cc:54] - xxx */
/*LOG(INFO , "xxx") 只会打印大于 INFO 等级的日志*/


#include <iostream>
#include <string>
#include <cstdio>
#include <time.h>

// 日志等级
#define INFO 0                  /*正常信息*/
#define DEBUG 1                 /*调试信息*/
#define WARNING 2               /*警告信息*/
#define ERROR 3                 /*错误信息*/
#define FATAL 4                 /*致命信息*/
#define DEFAULT_LOG_LEVEL DEBUG /*默认等级*/

std::string IntToString(unsigned int level)
{
    switch (level)
    {
    case INFO:
        return "INFO";
        break;
    case DEBUG:
        return "DEBUG";
        break;
    case WARNING:
        return "WARNING";
        break;
    case ERROR:
        return "ERROR";
        break;
    case FATAL:
        return "FATAL";
        break;

    default:
        return std::string();
        break;
    }
}

// 日志宏 -- ... 可变参数 ,  /*## __VA_ARGS  宏的可变参数 -- 允许外部使用时随便传参*/
#define LOG(level, format, ...)                                                                            \
    do                                                                                                     \
    {                                                                                                      \
        if (DEFAULT_LOG_LEVEL > level)                                                                     \
            break;                                                                                         \
        time_t t = time(NULL);                                                                             \
        struct tm *restime = localtime(&t);                                                                \
        char timebuf[64] = {0};                                                                            \
        strftime(timebuf, 63, "%Y-%m-%d %H:%M:%S", restime);                                               \
        fprintf(stdout, "[%s] [%s] [%s:%d] - " format "\n", timebuf, IntToString(level).c_str(), __FILE__, __LINE__, ##__VA_ARGS__); \
    } while (0)

#define IFLOG(format, ...) LOG(INFO, format, ##__VA_ARGS__)
#define DBGLOG(format, ...) LOG(DEBUG, format, ##__VA_ARGS__)
#define WINGLOG(format, ...) LOG(WARNING, format, ##__VA_ARGS__)
#define ERRLOG(format, ...) LOG(ERROR, format, ##__VA_ARGS__)
#define FALOG(format, ...) LOG(FATAL, format, ##__VA_ARGS__)

#endif